package com.yangguang.huahui.ui.activitys;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.ui.fragments.CollectFragment;
import com.yangguang.huahui.ui.fragments.MyQRCodeFragment;
import com.yangguang.huahui.ui.fragments.MyWendanFragment;

import org.apache.http.util.ByteArrayBuffer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/1.
 */

public class MySecondActivity extends BaseActivity{
    @BindView(R.id.tv_title)
    TextView tv_title;
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();


    @OnClick({R.id.iv_back})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
    @Override
    protected int initLayoutId() {
        return R.layout.activity_mysecond;
    }



    byte[] bitmapByte;
    @Override
    protected void initView() {

        switch (getIntent().getIntExtra("type", 0)){
            case 1://我的问答
                ft.add(R.id.mFL_content, new MyWendanFragment()).commit();
                break;
            case 2://我的二维码
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        bitmapByte = GetLocalOrNetBitmap(DataInfo.AVATARS);
                        MyQRCodeFragment myQRCodeFragment = new MyQRCodeFragment();
                        Bundle bundle = new Bundle();
                        bundle.putByteArray("byte[]", bitmapByte);
                        myQRCodeFragment.setArguments(bundle);
                        ft.add(R.id.mFL_content, myQRCodeFragment).commit();
                    }
                }).start();
                break;
            case 3://我的收藏
                ft.add(R.id.mFL_content, new CollectFragment()).commit();
                break;
        }
    }

    public static byte[] GetLocalOrNetBitmap(String url)
    {// 定义获取文件内容的URL
        URL myURL = null;
        try {
            myURL = new URL(url);

            // 打开URL链接
            URLConnection ucon = myURL.openConnection();
            // 使用InputStream，从URLConnection读取数据
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            // 用ByteArrayBuffer缓存
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }
            return baf.toByteArray();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void initData() {
        tv_title.setText(getIntent().getStringExtra("title"));
    }
}
